HW1

This homework is partly accomplished by ai Question 1

library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(readr)
library(ggplot2)
library(lubridate)

Attaching package: 'lubridate'
The following objects are masked from 'package:base':

    date, intersect, setdiff, union
library(leaflet)
library(sf)
Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
library(tigris)
To enable caching of data, set `options(tigris_use_cache = TRUE)`
in your R script or .Rprofile.
library(tidyr)
data_2002 <- read_csv("C:/Users/27753/Desktop/PM566/PM2.5 2002.csv")
Rows: 15976 Columns: 22
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (12): Date, Source, Site ID, Units, Local Site Name, AQS Parameter Descr...
dbl (10): POC, Daily Mean PM2.5 Concentration, Daily AQI Value, Daily Obs Co...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data_2022 <- read_csv("C:/Users/27753/Desktop/PM566/PM2.5 2022.csv")
Rows: 59918 Columns: 22
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (12): Date, Source, Site ID, Units, Local Site Name, AQS Parameter Descr...
dbl (10): POC, Daily Mean PM2.5 Concentration, Daily AQI Value, Daily Obs Co...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
dim(data_2002)
[1] 15976    22
dim(data_2022)
[1] 59918    22
summary(data_2002$`Daily Mean PM2.5 Concentration`)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.00    7.00   12.00   16.12   20.50  104.30 
summary(data_2022$`Daily Mean PM2.5 Concentration`)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 -6.700   4.100   6.800   8.414  10.700 302.500 
head(data_2002)
# A tibble: 6 × 22
  Date     Source `Site ID`   POC Daily Mean PM2.5 Con…¹ Units `Daily AQI Value`
  <chr>    <chr>  <chr>     <dbl>                  <dbl> <chr>             <dbl>
1 01/05/2… AQS    060010007     1                   25.1 ug/m…                81
2 01/06/2… AQS    060010007     1                   31.6 ug/m…                93
3 01/08/2… AQS    060010007     1                   21.4 ug/m…                74
4 01/11/2… AQS    060010007     1                   25.9 ug/m…                82
5 01/14/2… AQS    060010007     1                   34.5 ug/m…                98
6 01/17/2… AQS    060010007     1                   41   ug/m…               115
# ℹ abbreviated name: ¹​`Daily Mean PM2.5 Concentration`
# ℹ 15 more variables: `Local Site Name` <chr>, `Daily Obs Count` <dbl>,
#   `Percent Complete` <dbl>, `AQS Parameter Code` <dbl>,
#   `AQS Parameter Description` <chr>, `Method Code` <dbl>,
#   `Method Description` <chr>, `CBSA Code` <dbl>, `CBSA Name` <chr>,
#   `State FIPS Code` <chr>, State <chr>, `County FIPS Code` <chr>,
#   County <chr>, `Site Latitude` <dbl>, `Site Longitude` <dbl>
head(data_2022)
# A tibble: 6 × 22
  Date     Source `Site ID`   POC Daily Mean PM2.5 Con…¹ Units `Daily AQI Value`
  <chr>    <chr>  <chr>     <dbl>                  <dbl> <chr>             <dbl>
1 01/01/2… AQS    060010007     3                   12.7 ug/m…                58
2 01/02/2… AQS    060010007     3                   13.9 ug/m…                60
3 01/03/2… AQS    060010007     3                    7.1 ug/m…                39
4 01/04/2… AQS    060010007     3                    3.7 ug/m…                21
5 01/05/2… AQS    060010007     3                    4.2 ug/m…                23
6 01/06/2… AQS    060010007     3                    3.8 ug/m…                21
# ℹ abbreviated name: ¹​`Daily Mean PM2.5 Concentration`
# ℹ 15 more variables: `Local Site Name` <chr>, `Daily Obs Count` <dbl>,
#   `Percent Complete` <dbl>, `AQS Parameter Code` <dbl>,
#   `AQS Parameter Description` <chr>, `Method Code` <dbl>,
#   `Method Description` <chr>, `CBSA Code` <dbl>, `CBSA Name` <chr>,
#   `State FIPS Code` <chr>, State <chr>, `County FIPS Code` <chr>,
#   County <chr>, `Site Latitude` <dbl>, `Site Longitude` <dbl>
tail(data_2002)
# A tibble: 6 × 22
  Date     Source `Site ID`   POC Daily Mean PM2.5 Con…¹ Units `Daily AQI Value`
  <chr>    <chr>  <chr>     <dbl>                  <dbl> <chr>             <dbl>
1 12/10/2… AQS    061131003     1                     15 ug/m…                62
2 12/13/2… AQS    061131003     1                     15 ug/m…                62
3 12/22/2… AQS    061131003     1                      1 ug/m…                 6
4 12/25/2… AQS    061131003     1                     23 ug/m…                77
5 12/28/2… AQS    061131003     1                      5 ug/m…                28
6 12/31/2… AQS    061131003     1                      6 ug/m…                33
# ℹ abbreviated name: ¹​`Daily Mean PM2.5 Concentration`
# ℹ 15 more variables: `Local Site Name` <chr>, `Daily Obs Count` <dbl>,
#   `Percent Complete` <dbl>, `AQS Parameter Code` <dbl>,
#   `AQS Parameter Description` <chr>, `Method Code` <dbl>,
#   `Method Description` <chr>, `CBSA Code` <dbl>, `CBSA Name` <chr>,
#   `State FIPS Code` <chr>, State <chr>, `County FIPS Code` <chr>,
#   County <chr>, `Site Latitude` <dbl>, `Site Longitude` <dbl>
tail(data_2022)
# A tibble: 6 × 22
  Date     Source `Site ID`   POC Daily Mean PM2.5 Con…¹ Units `Daily AQI Value`
  <chr>    <chr>  <chr>     <dbl>                  <dbl> <chr>             <dbl>
1 12/01/2… AQS    061131003     1                    3.4 ug/m…                19
2 12/07/2… AQS    061131003     1                    3.8 ug/m…                21
3 12/13/2… AQS    061131003     1                    6   ug/m…                33
4 12/19/2… AQS    061131003     1                   34.8 ug/m…                99
5 12/25/2… AQS    061131003     1                   23.2 ug/m…                77
6 12/31/2… AQS    061131003     1                    1   ug/m…                 6
# ℹ abbreviated name: ¹​`Daily Mean PM2.5 Concentration`
# ℹ 15 more variables: `Local Site Name` <chr>, `Daily Obs Count` <dbl>,
#   `Percent Complete` <dbl>, `AQS Parameter Code` <dbl>,
#   `AQS Parameter Description` <chr>, `Method Code` <dbl>,
#   `Method Description` <chr>, `CBSA Code` <dbl>, `CBSA Name` <chr>,
#   `State FIPS Code` <chr>, State <chr>, `County FIPS Code` <chr>,
#   County <chr>, `Site Latitude` <dbl>, `Site Longitude` <dbl>
str(data_2002)
spc_tbl_ [15,976 × 22] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
 $ Date                          : chr [1:15976] "01/05/2002" "01/06/2002" "01/08/2002" "01/11/2002" ...
 $ Source                        : chr [1:15976] "AQS" "AQS" "AQS" "AQS" ...
 $ Site ID                       : chr [1:15976] "060010007" "060010007" "060010007" "060010007" ...
 $ POC                           : num [1:15976] 1 1 1 1 1 1 1 1 1 1 ...
 $ Daily Mean PM2.5 Concentration: num [1:15976] 25.1 31.6 21.4 25.9 34.5 41 29.3 15 18.8 37.9 ...
 $ Units                         : chr [1:15976] "ug/m3 LC" "ug/m3 LC" "ug/m3 LC" "ug/m3 LC" ...
 $ Daily AQI Value               : num [1:15976] 81 93 74 82 98 115 89 62 69 107 ...
 $ Local Site Name               : chr [1:15976] "Livermore" "Livermore" "Livermore" "Livermore" ...
 $ Daily Obs Count               : num [1:15976] 1 1 1 1 1 1 1 1 1 1 ...
 $ Percent Complete              : num [1:15976] 100 100 100 100 100 100 100 100 100 100 ...
 $ AQS Parameter Code            : num [1:15976] 88101 88101 88101 88101 88101 ...
 $ AQS Parameter Description     : chr [1:15976] "PM2.5 - Local Conditions" "PM2.5 - Local Conditions" "PM2.5 - Local Conditions" "PM2.5 - Local Conditions" ...
 $ Method Code                   : num [1:15976] 120 120 120 120 120 120 120 120 120 120 ...
 $ Method Description            : chr [1:15976] "Andersen RAAS2.5-300 PM2.5 SEQ w/WINS" "Andersen RAAS2.5-300 PM2.5 SEQ w/WINS" "Andersen RAAS2.5-300 PM2.5 SEQ w/WINS" "Andersen RAAS2.5-300 PM2.5 SEQ w/WINS" ...
 $ CBSA Code                     : num [1:15976] 41860 41860 41860 41860 41860 ...
 $ CBSA Name                     : chr [1:15976] "San Francisco-Oakland-Hayward, CA" "San Francisco-Oakland-Hayward, CA" "San Francisco-Oakland-Hayward, CA" "San Francisco-Oakland-Hayward, CA" ...
 $ State FIPS Code               : chr [1:15976] "06" "06" "06" "06" ...
 $ State                         : chr [1:15976] "California" "California" "California" "California" ...
 $ County FIPS Code              : chr [1:15976] "001" "001" "001" "001" ...
 $ County                        : chr [1:15976] "Alameda" "Alameda" "Alameda" "Alameda" ...
 $ Site Latitude                 : num [1:15976] 37.7 37.7 37.7 37.7 37.7 ...
 $ Site Longitude                : num [1:15976] -122 -122 -122 -122 -122 ...
 - attr(*, "spec")=
  .. cols(
  ..   Date = col_character(),
  ..   Source = col_character(),
  ..   `Site ID` = col_character(),
  ..   POC = col_double(),
  ..   `Daily Mean PM2.5 Concentration` = col_double(),
  ..   Units = col_character(),
  ..   `Daily AQI Value` = col_double(),
  ..   `Local Site Name` = col_character(),
  ..   `Daily Obs Count` = col_double(),
  ..   `Percent Complete` = col_double(),
  ..   `AQS Parameter Code` = col_double(),
  ..   `AQS Parameter Description` = col_character(),
  ..   `Method Code` = col_double(),
  ..   `Method Description` = col_character(),
  ..   `CBSA Code` = col_double(),
  ..   `CBSA Name` = col_character(),
  ..   `State FIPS Code` = col_character(),
  ..   State = col_character(),
  ..   `County FIPS Code` = col_character(),
  ..   County = col_character(),
  ..   `Site Latitude` = col_double(),
  ..   `Site Longitude` = col_double()
  .. )
 - attr(*, "problems")=<externalptr> 
str(data_2022)
spc_tbl_ [59,918 × 22] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
 $ Date                          : chr [1:59918] "01/01/2022" "01/02/2022" "01/03/2022" "01/04/2022" ...
 $ Source                        : chr [1:59918] "AQS" "AQS" "AQS" "AQS" ...
 $ Site ID                       : chr [1:59918] "060010007" "060010007" "060010007" "060010007" ...
 $ POC                           : num [1:59918] 3 3 3 3 3 3 3 3 3 3 ...
 $ Daily Mean PM2.5 Concentration: num [1:59918] 12.7 13.9 7.1 3.7 4.2 3.8 2.3 6.9 13.6 11.2 ...
 $ Units                         : chr [1:59918] "ug/m3 LC" "ug/m3 LC" "ug/m3 LC" "ug/m3 LC" ...
 $ Daily AQI Value               : num [1:59918] 58 60 39 21 23 21 13 38 59 55 ...
 $ Local Site Name               : chr [1:59918] "Livermore" "Livermore" "Livermore" "Livermore" ...
 $ Daily Obs Count               : num [1:59918] 1 1 1 1 1 1 1 1 1 1 ...
 $ Percent Complete              : num [1:59918] 100 100 100 100 100 100 100 100 100 100 ...
 $ AQS Parameter Code            : num [1:59918] 88101 88101 88101 88101 88101 ...
 $ AQS Parameter Description     : chr [1:59918] "PM2.5 - Local Conditions" "PM2.5 - Local Conditions" "PM2.5 - Local Conditions" "PM2.5 - Local Conditions" ...
 $ Method Code                   : num [1:59918] 170 170 170 170 170 170 170 170 170 170 ...
 $ Method Description            : chr [1:59918] "Met One BAM-1020 Mass Monitor w/VSCC" "Met One BAM-1020 Mass Monitor w/VSCC" "Met One BAM-1020 Mass Monitor w/VSCC" "Met One BAM-1020 Mass Monitor w/VSCC" ...
 $ CBSA Code                     : num [1:59918] 41860 41860 41860 41860 41860 ...
 $ CBSA Name                     : chr [1:59918] "San Francisco-Oakland-Hayward, CA" "San Francisco-Oakland-Hayward, CA" "San Francisco-Oakland-Hayward, CA" "San Francisco-Oakland-Hayward, CA" ...
 $ State FIPS Code               : chr [1:59918] "06" "06" "06" "06" ...
 $ State                         : chr [1:59918] "California" "California" "California" "California" ...
 $ County FIPS Code              : chr [1:59918] "001" "001" "001" "001" ...
 $ County                        : chr [1:59918] "Alameda" "Alameda" "Alameda" "Alameda" ...
 $ Site Latitude                 : num [1:59918] 37.7 37.7 37.7 37.7 37.7 ...
 $ Site Longitude                : num [1:59918] -122 -122 -122 -122 -122 ...
 - attr(*, "spec")=
  .. cols(
  ..   Date = col_character(),
  ..   Source = col_character(),
  ..   `Site ID` = col_character(),
  ..   POC = col_double(),
  ..   `Daily Mean PM2.5 Concentration` = col_double(),
  ..   Units = col_character(),
  ..   `Daily AQI Value` = col_double(),
  ..   `Local Site Name` = col_character(),
  ..   `Daily Obs Count` = col_double(),
  ..   `Percent Complete` = col_double(),
  ..   `AQS Parameter Code` = col_double(),
  ..   `AQS Parameter Description` = col_character(),
  ..   `Method Code` = col_double(),
  ..   `Method Description` = col_character(),
  ..   `CBSA Code` = col_double(),
  ..   `CBSA Name` = col_character(),
  ..   `State FIPS Code` = col_character(),
  ..   State = col_character(),
  ..   `County FIPS Code` = col_character(),
  ..   County = col_character(),
  ..   `Site Latitude` = col_double(),
  ..   `Site Longitude` = col_double()
  .. )
 - attr(*, "problems")=<externalptr> 
hist(data_2002$`Daily Mean PM2.5 Concentration`)

hist(data_2022$`Daily Mean PM2.5 Concentration`)

Summary: dimensions:The data of 2002 to 2022,both have 22 columns which means the same variable, but the rows increase from 15976 to 59918, this means they conducted more data in 2022 than in 2002. headers/footers:From those two it can be seen the data upload correctly and all variables go well variable names and types: It can be seen the variables name and types for 2022 and 2002 ares the same, which will help to continue for the next part. key variables:The key variables is the concentration of PM2.5, and from the mean it can be seen the mean of concentration of PM2.5 in 2022 in lower than the 2002, and from the histogram, both of the two years’ concentration seem to be normal distribution, and there is obvious change of the distribution in the data of 2002 and 2022.

Question 2

data_2002 <- data_2002 %>% mutate(year = 2002)
data_2022 <- data_2022 %>% mutate(year = 2022)
combined_data <- bind_rows(data_2002, data_2022)
combined_data <- combined_data %>%
  rename(
    pm25_concentration = `Daily Mean PM2.5 Concentration`
  )

Question 3

site_locations <- combined_data %>%
  select(`Site Longitude`,`Site Latitude`,year,`Site ID`)

leaflet(data = site_locations) %>%
  addTiles() %>%
  addCircles(
    lng = ~`Site Longitude`,
    lat = ~`Site Latitude`,
    color = ~ifelse(year == 2002, "blue", "red"),
    radius = 500,
    popup = ~paste("Site ID:", `Site ID`, "<br>Year:", year)
  )

It can be seen on the results that, most of the sites located at california from 2002 to 2022, the 2002 sites seem to equally located on different location in the california, and the 2022 sites seem to locate most of them on north and south part of california, with only a little part locate on the middle part. Comparing to 2002,the 2022 have much more location to check, and the density located in each location increase more than before.

Question 4

missing_summary <- combined_data %>%
  group_by(year) %>%
  summarise(
    total_obs = n(),
    missing_pm2.5 = sum(is.na(pm25_concentration)),
    prop_missing = missing_pm2.5 / total_obs
  )
print(missing_summary)
# A tibble: 2 × 4
   year total_obs missing_pm2.5 prop_missing
  <dbl>     <int>         <int>        <dbl>
1  2002     15976             0            0
2  2022     59918             0            0
implausible_summary <- combined_data %>%
  filter(pm25_concentration < 0) %>%
  group_by(year) %>%
  tally(name = "implausible_count")

print(implausible_summary)
# A tibble: 1 × 2
   year implausible_count
  <dbl>             <int>
1  2022               215

The missing value is 0, and the implausible value for 2002 is 0 for 2022 is 215, the portion of missing/implausible value of 2022= 215/75894=0.28%,the portion of missing/implausible value of 2002= 0 It can see there is no missing value in this research, but in year 2022 there happen to be 215 value under zero that is implausible, it is concerned that although 2022 have much more data than 2002, there also had some wrong data decreasing the credibility of analysis and conclusion using the data of 2022.

Question 5 (1)

ggplot(data = combined_data, aes(x = factor(year), y = pm25_concentration)) +
  geom_boxplot() +
  labs(
    title = "California PM2.5 Concentrations (2002 vs 2022)",
    x = "Year",
    y = "PM2.5 (µg/m³)"
  ) +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))

state_summary <- combined_data %>%
  group_by(year) %>%
  summarise(
    mean_pm2.5 = mean(pm25_concentration, na.rm = TRUE),
    median_pm2.5 = median(pm25_concentration, na.rm = TRUE),
    iqr_pm2.5 = IQR(pm25_concentration, na.rm = TRUE),
    rangepm2.5 = range(pm25_concentration, na.rm = TRUE)
  )
Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
dplyr 1.1.0.
ℹ Please use `reframe()` instead.
ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
  always returns an ungrouped data frame and adjust accordingly.
`summarise()` has grouped output by 'year'. You can override using the
`.groups` argument.
print(state_summary)
# A tibble: 4 × 5
# Groups:   year [2]
   year mean_pm2.5 median_pm2.5 iqr_pm2.5 rangepm2.5
  <dbl>      <dbl>        <dbl>     <dbl>      <dbl>
1  2002      16.1          12        13.5        0  
2  2002      16.1          12        13.5      104. 
3  2022       8.41          6.8       6.6       -6.7
4  2022       8.41          6.8       6.6      302. 

For the state, the mean and median of PM2.5concentration has a obvious decrease, which means that overall the California PM2.5concentration really decrease in the past 20 years, but from the boxplot and range, it can be seen that the range of 2022 increase including many outliers and noise, that is may be the data itself has problem or the maximum level of PM2.5concentration really increase in the past 20 years (2)

# Download California county boundaries
ca_counties <- counties(state = "CA", class = "sf")
Retrieving data for the year 2024

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |============================================                          |  64%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
# Calculate county-level average PM2.5 for 2002 and 2022
county_data_2002 <- combined_data %>%
  filter(year == 2002) %>%
  group_by(County) %>%
  summarise(mean_pm2.5 = mean(pm25_concentration, na.rm = TRUE), .groups = "drop")
county_data_2022 <- combined_data %>%
  filter(year == 2022) %>%
  group_by(County) %>%
  summarise(mean_pm2.5 = mean(pm25_concentration, na.rm = TRUE), .groups = "drop")

# Merge data with county boundaries
ca_counties_2002 <- ca_counties %>%
  left_join(county_data_2002, by = c("NAME" = "County")) %>%
  mutate(mean_pm2.5_2002 = mean_pm2.5) %>%
  select(-mean_pm2.5)

ca_counties_2022 <- ca_counties %>%
  left_join(county_data_2022, by = c("NAME" = "County")) %>%
  mutate(mean_pm2.5_2022 = mean_pm2.5) %>%
  select(-mean_pm2.5)

# Calculate the difference for visualization
ca_counties_diff <- ca_counties_2002 %>%
  left_join(as_tibble(ca_counties_2022), by = c("NAME", "geometry")) %>%
  mutate(pm2.5_change = mean_pm2.5_2022 - mean_pm2.5_2002)

# Create color palette for the change
diff_pal <- colorNumeric(
  palette = "RdYlGn",
  domain = ca_counties_diff$pm2.5_change
)

# County-level leaflet map
leaflet(ca_counties_diff) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor = ~diff_pal(pm2.5_change),
    fillOpacity = 0.8,
    color = "white",
    weight = 1,
    popup = ~paste0("County: ", NAME, "<br>PM2.5 Change: ", round(pm2.5_change, 2))
  ) %>%
  addLegend(
    pal = diff_pal,
    values = ~pm2.5_change,
    title = "PM2.5 Change (µg/m³)<br>2002-2022"
  )
Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84'

The interactive choropleth map shows the average change in PM2.5 concentration for each county. By hovering over or clicking on a county, we can see the specific magnitude of the change. Counties shaded in green indicate a decrease in PM2.5, while those in red indicate an increase. This visualization revealing regional variations that a simple state-level average would miss. It is shown the some county in the south of the California actually has a increase in the PM2.5 concentration, San Bernardino makes the most increasing PM2.5 concentration, and the north of the California’s air is better than before, Trinity most the most decreasing PM2.5concentration. (3)

la_county_data <- combined_data %>%
  filter(County == "Los Angeles")

# Calculate site-level average PM2.5 for LA County
la_site_averages <- la_county_data %>%
  group_by(`Site Longitude`,`Site Latitude`,year,`Site ID`) %>%
  summarise(
    mean_pm2.5 = mean(pm25_concentration, na.rm = TRUE),
    .groups = "drop"
  )

# Create color palette for the mean PM2.5
la_pal <- colorNumeric(
  palette = "viridis",
  domain = la_site_averages$mean_pm2.5
)

# LA County leaflet map
leaflet(la_site_averages) %>%
  addTiles(group = "OSM") %>%
  addProviderTiles(providers$CartoDB.Positron, group = "CartoDB") %>%
  addLayersControl(
    baseGroups = c("OSM", "CartoDB"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  addCircleMarkers(
    data = filter(la_site_averages, year == 2002),
    lng = ~`Site Longitude`,
    lat = ~`Site Latitude`,
    color = "blue",
    radius = 5,
    fillOpacity = 0.8,
    popup = ~paste0("Site: ", `Site ID`, "<br>Year: 2002<br>Mean PM2.5: ", round(mean_pm2.5, 2)),
    group = "2002"
  ) %>%
  addCircleMarkers(
    data = filter(la_site_averages, year == 2022),
    lng = ~`Site Longitude`,
    lat = ~`Site Latitude`,
    color = "red",
    radius = 5,
    fillOpacity = 0.8,
    popup = ~paste0("Site: ", `Site ID`, "<br>Year: 2022<br>Mean PM2.5: ", round(mean_pm2.5, 2)),
    group = "2022"
  ) %>%
  addLayersControl(
    overlayGroups = c("2002", "2022"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  setView(lng = -118.2437, lat = 34.0522, zoom = 9)

The interactive map for Los Angeles County shows individual monitoring sites. By using layers, we can toggle between the 2002 and 2022 data to visually compare the distribution of sites and average PM2.5 concentrations. We can observe if some sites, which are more indicative of specific urban or industrial areas, experienced different magnitudes of change compared to others. The use of distinct colors for each year (blue for 2002, red for 2022) makes it easy to see where monitoring was active and how conditions have changed.It is seen that the data of 2022 contain more area of LA than 2002 and thus can get more complete condition around the LA, from the mean LA’s PM2.5concentration is decreasing from 2002 to 2022. (The homework is partly accomplished in ai )